package ph.com.gs3.formalistics.view.document.fields;

import ph.com.gs3.formalistics.global.constants.Constants;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.TextTypeFieldView;
import android.content.Context;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

public class TextField extends TextTypeFieldView {

	private TextFieldType textFieldType;

	public static enum TextFieldType {
		MULTILINE, NORMAL
	}

	public TextField(Context context, FormFieldData formField) {
		super(context, formField);
		this.textFieldType = TextFieldType.NORMAL;

		createLabeledDefaultContainer(formField.getLabel(), getFieldId() + "_container");
		createView();
	}

	public TextField(Context context, FormFieldData formField, TextFieldType textFieldType) {
		super(context, formField);
		this.textFieldType = textFieldType;

		createLabeledDefaultContainer(formField.getLabel(), getFieldId() + "_container");
		createView();
	}

	private void createView() {

		setEditTextValueField(new EditText(getContext()));
		getEditTextValueField().setLayoutParams(lpFillWidth);
		getEditTextValueField().setTag(formField.getId());

		getEditTextValueField().setTypeface(Constants.getApplicationInstance().getGlobalTypeface());

		setTextFieldType(textFieldType);

		if (formField.getPlaceHolder() != null) {
			getEditTextValueField().setHint(formField.getPlaceHolder());
		}

		if (formField.getValue() != null) {
			getEditTextValueField().setText(formField.getValue());
		}

		// set enabled/disabled
		getEditTextValueField().setEnabled(isEnabled);
		this.addView(getEditTextValueField());
	}

	public void setTextFieldType(TextFieldType textFieldType) {
		this.textFieldType = textFieldType;
		if (textFieldType == TextFieldType.MULTILINE) {
			getEditTextValueField().setSingleLine(false);
			getEditTextValueField().setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
		} else {
			getEditTextValueField().setSingleLine(true);
		}
	}

	public TextFieldType getTextFieldType() {
		return textFieldType;
	}

	@Override
	public void setEnabled(boolean isEnabled) {
		getEditTextValueField().setEnabled(isEnabled);
	}

	@Override
	public String getValue() {
		return getEditTextValueField().getText().toString();
	}

	@Override
	public void setValue(String value) {
		getEditTextValueField().setText(value);
	}

}
